//
//  DiscussViewController.m
//  Movie
//
//  Created by tengyang on 15/7/27.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "DiscussViewController.h"
#import "UIImageView+WebCache.h"
#import "common.h"
#import "DiscussModal.h"
#import "DataService.h"
#import "DiscussTableViewCell.h"
#import "UIViewExt.h"

@interface DiscussViewController ()
{
    BOOL flags[100];
}
@end

@implementation DiscussViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTableView];
    [self _loadCollectionData];
    [self _createScrollView];
    [self _createTopView];
    
}
- (void)_createTopView
{
    _topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 150)];
    _topView.backgroundColor=[UIColor blackColor];
    UIImageView *image=[[UIImageView alloc]init];
    [image sd_setImageWithURL:[NSURL URLWithString:@"http://img31.mtime.cn/mt/2012/06/28/131128.94272291.jpg"]];
    image.frame=CGRectMake(10, 10, 100, 140);
    [_topView addSubview:image];
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(120, 10, 200, 30)];
    title.text=@"摩尔庄园2海妖宝藏";
    title.textColor=[UIColor orangeColor];
    title.font=[UIFont systemFontOfSize:20];
    
    [_topView addSubview:title];
    
    UILabel *director=[[UILabel alloc]initWithFrame:CGRectMake(120, 50, 200, 20)];
    director.text=@"导演:刘可欣";
    director.textColor=[UIColor whiteColor];
    [_topView addSubview:director];
    
    UILabel *actors=[[UILabel alloc]initWithFrame:CGRectMake(120, 70, 200, 20)];
    actors.textColor=[UIColor whiteColor];
    actors.text=@"演员:阿黄,阿龟,阿宇,阿红,凤姐";
    [_topView addSubview:actors];
    
    UILabel *type=[[UILabel alloc]initWithFrame:CGRectMake(120, 90, 200, 20)];
    type.text=@"类型:动画/动作/奇幻/冒险";
    type.textColor=[UIColor whiteColor];
    [_topView addSubview:type];
    
    
    UILabel *date=[[UILabel alloc]initWithFrame:CGRectMake(120, 110, 200, 20)];
    date.text=@"中国 2012-7-5";
    date.textColor=[UIColor whiteColor];
    [_topView addSubview:date];
    
    [_headView addSubview:_topView];
}
- (void)_createScrollView
{
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 160, kWidth-20, 100)];
    scrollView.contentSize=CGSizeMake((kWidth-20)+50, 100);
    scrollView.backgroundColor=[UIColor blackColor];
    for (int i=0; i<4; i++) {
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(i*kWidth/4+10*i, 5, kWidth/4, 90)];
        [image sd_setImageWithURL:[NSURL URLWithString:_imagesArray[i]]];
        [scrollView addSubview:image];
    }
    
    
    
    [_headView addSubview:scrollView];
}

- (void)_createTableView{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 260)];
    _headView.backgroundColor=[UIColor blackColor];
    _tableView.tableHeaderView=_headView;
    [self.view addSubview:_tableView];
    
    
    [self _loadTableData];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    
   
    UINib *nib=[UINib nibWithNibName:@"DiscussTableViewCell" bundle:[NSBundle mainBundle]];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
    
    
    
}

- (void)_loadTableData
{
    _modalArray=[[NSMutableArray alloc]init];
    NSDictionary *temp=[DataService getJsonDataFromFile:@"movie_comment.json"];
    NSArray *list=[temp objectForKey:@"list"];
    for (NSDictionary *dic in list) {
        DiscussModal *modal=[[DiscussModal alloc]init];
        modal.content=[dic objectForKey:@"content"];
        modal.nickname=[dic objectForKey:@"nickname"];
        modal.rating=[dic objectForKey:@"rating"];
        modal.userImage=[dic objectForKey:@"userImage"];
        
        [_modalArray addObject:modal];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modalArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    DiscussTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    
    
    DiscussModal *modal=_modalArray[indexPath.row];
    cell.modal=modal;
    cell.textLabel.numberOfLines=0;
    


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    flags[indexPath.row]=!flags[indexPath.row];
    
    [_tableView reloadData];

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DiscussModal *modal=_modalArray[indexPath.row];
    NSString *content=modal.content;
    CGSize label=[content sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(200.0, 5000) lineBreakMode:UILineBreakModeCharacterWrap];
    
    

    
    if (flags[indexPath.row]==YES) {
        return label.height+60;
        
    }
    return 80;
    
}



- (void)_loadCollectionData
{
    NSDictionary *dic=[DataService getJsonDataFromFile:@"movie_detail.json"];
    _imagesArray=[dic objectForKey:@"images"];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
